import {defineStore} from 'pinia';
import {throttle} from '@/utils/countdown-util';

export const musicStore = defineStore('musicStore', {
    state: () => ({
        // 播放模式 单曲循环 列表循环 顺序播放 随机播放
        singleCirculation: ['singleCirculation', 'tableCirculation', 'sequentialPlay', 'randomPlay'],
        player: '', // 播放器
        currentPlayMusic: { // 当前播放音乐的信息
            name: '', // 音乐名称
            url: '', // 音乐地址
            author: '', // 歌手
            pic: '', // 封面
            time: 0, // 时长
        },
        playListsList: [],// 歌单的列表
        playerSetting: { // 播放器设置
            currentDuration: 0, // 当前播放器时段
            currentTime: 0,
            playing: '',
            volume: 50, // 音量
            currentListName: 'likeMusicList', // 当前播放列表
            playMode: 'sequentialPlay', // 播放模式 - 顺序播放
            previousIsClick: true, // 可以点击上一首
            nextIsClick: true, // 可以点击下一首
            playIsClick: true, // 可以点播放
            playIndex: 0, // 当前播放歌曲在列表中的索引
            playName: '', // 当前播放歌曲的名称
            playState: false, // 当前播放状态
            playSchedule: 0, // 当前播放器进度
            playLoadeddata: false, // 音乐加载状态
            playSongStatus: false, // 播放歌单状态
        },
    }),
    // Getter 完全等同于 Store 状态的计算值，计算state中的值
    getters: {},
    // 设置state中的基本信息
    actions: {
        /**
         * 初始化播放器
         *
         */
        initMusic(container) {
            this.player = container;
            this.setPreviousAndNextClick();
            this.playerSetting.playState = false;
            this.playerSetting.playSchedule = 0;
            this.player.volume = this.playerSetting.volume / 100;
        },

        /**
         * 播放
         *
         */
        async play(index) {
            try {
                if (index || index === 0 || !this.currentPlayMusic.url) {
                    (index || index === 0) && (this.playerSetting.playIndex = index);
                    const currentListName = this.playerSetting.currentListName;
                    let currentPlay = this[currentListName][this.playerSetting.playIndex];
                    // let params = {
                    //     id:currentPlay.id,
                    //     level:'standard'
                    // };
                    // const resUrl = await musicList.getSongUrlv1(params);
                    // if(resUrl.code !== 200) throw new Error(resUrl.message);
                    // // 因API缘故，需将音乐信息和地址组合
                    // currentPlay = {...currentPlay, ...resUrl.data[0]};
                    // this.setCurrentMusicInfo(currentPlay);
                }
                // 刷新后不初始化歌曲
                // (!this.player.src && !index) && this.setCurrentMusicInfo({});
                // (this.player) && this.player.play();
                // this.playerSetting.play_state = true;
                // this.setPreviousAndNextClick();
            } catch (e) {
                console.error(e.message);
            }
        },

        /**
         * 更新当前歌曲信息
         *
         */
        setCurrentMusicInfo(info) {
            this.currentPlayMusic = Object.assign(this.currentPlayMusic, info);
            this.currentPlayMusic.author = this.currentPlayMusic.ar[0].name;
            this.currentPlayMusic.picUrl = this.currentPlayMusic.al.picUrl;
            if (this.player && this.currentPlayMusic.url) {
                this.player.src = this.currentPlayMusic.url;
                this.playerSetting.playName = this.currentPlayMusic.name;
            }
        },

        /**
         * 获取音乐当前播放的时段
         *
         */
        updateTime() {
            if (this.player) {
                this.playerSetting.currentDuration = ~~this.player.currentTime;
                this.playerSetting.playSchedule = parseFloat((this.playerSetting.currentDuration / (this.currentPlayMusic.time / 1000)) * 100).toFixed(2);
            }
        },

        /**
         * 设置音量
         *
         */
        settingVolume(volume) {
            if (this.player) {
                this.playerSetting.volume = parseInt(volume * 100);
                this.player.volume = volume;
            }
        },

        /**
         * 监听音乐播放结束
         *
         */
        endPlayback() {
            this.playerSetting.currentDuration = 0;
            this.playerSetting.playState = false;
            // 单曲循环
            if (this.playerSetting.playMode === 'singleCirculation') {
                this.play(this.playerSetting.playIndex)
            } else { // 播放器默认就是列表循环 列表循环
                this.next();
            }
        },

        /**
         * 切换播放模式
         *
         */
        changePlayMode(mode) {
            this.playerSetting.playMode = mode;
        },

        /**
         * 上一首
         *
         */
        previous() {
            // 防抖
            if (!throttle()) return;
            let currentListName = this.playerSetting.currentListName;
            this.playerSetting.playIndex -= 1;
            this.playerSetting.playIndex < 0 && (this.playerSetting.playIndex = this[currentListName].length - 1);
            // 无音源 跳过
            (this[currentListName][this.playerSetting.playIndex].noCopyrightRcmd) && (this.playerSetting.playIndex -= 1);
            this.play(this.playerSetting.playIndex);
        },

        /**
         * 下一首
         *
         */
         next() {
            // 防抖
            if(!throttle()) return;
            // 喜欢的音乐
            let currentListName = this.playerSetting.currentListName;
            this.playerSetting.playIndex += 1;
            (this.playerSetting.playIndex > this[currentListName].length-1) && (this.playerSetting.playIndex = 0);
            // 无音源 跳过
            (this[currentListName][this.playerSetting.playIndex].noCopyrightRcmd) && (this.playerSetting.playIndex += 1);
            this.play(this.playerSetting.playIndex);
        },


        /**
         * 设置上下切换按钮可以点击
         *
         */
        setPreviousAndNextClick() {
            this.playerSetting.nextIsClick = this.playerSetting.playIndex !== -1;
            this.playerSetting.previousIsClick = this.playerSetting.playIndex !== -1;
        },
    },

    persist: {},
})
